<template>
  <div class="app-container">
    <el-form :inline="true" size="small" label-width="100">
      <el-form-item>
        <el-button size="small" class="btn-add" @click="handleAdd">添加</el-button>
      </el-form-item>
    </el-form>
    <div class="table-container">
      <el-table ref="flashSessionTable" height="100%" :data="list" style="width: 100%" v-loading="listLoading" border>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="秒杀时间段名称" align="center">
          <template #default="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="每日开始时间" align="center">
          <template #default="scope">{{ formatTimeHours(scope.row.startTime) }}</template>
        </el-table-column>
        <el-table-column label="每日结束时间" align="center">
          <template #default="scope">{{ formatTimeHours(scope.row.endTime) }}</template>
        </el-table-column>
        <el-table-column label="启用" align="center" width="100">
          <template #default="scope">
            <el-switch
              size="small"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
              @change="handleStatusChange(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="handleUpdate(scope.row)">编辑 </el-button>
            <el-button type="danger" size="small" link @click="handleDelete(scope.row)">删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="添加时间段" v-model="dialogVisible" width="40%">
      <el-form :model="flashSession" ref="flashSessionForm" label-width="auto" size="small">
        <el-form-item label="秒杀时间段名称：">
          <el-input v-model="flashSession.name"></el-input>
        </el-form-item>
        <el-form-item label="每日开始时间：">
          <el-time-picker is-range v-model="timeRange" start-placeholder="开始时间" end-placeholder="结束时间"> </el-time-picker>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-switch size="small" :active-value="1" :inactive-value="0" v-model="flashSession.status"> </el-switch>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="handleDialogConfirm" size="small">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { fetchList, updateStatus, deleteSession, createSession, updateSession } from '@/api/flashSession'
import { ref, computed } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRoute } from 'vue-router'
import type { FlashSession } from '@/models/flashSession'
import { formatTimeHours } from '@/utils/format'

defineOptions({ name: 'FlashSessionList' })
const route = useRoute()
const list = ref<FlashSession[]>([])
const listLoading = ref(false)
const dialogVisible = ref(false)
const flashSession = ref<FlashSession>({})
getList()

function handleAdd() {
  dialogVisible.value = true
  flashSession.value = {}
}

const timeRange = computed({
  get() {
    const { startTime, endTime } = flashSession.value
    return [startTime, endTime]
  },
  set(val = []) {
    flashSession.value.startTime = val[0]
    flashSession.value.endTime = val[1]
  },
})

async function handleStatusChange(row: FlashSession) {
  await updateStatus({ status: row.status, id: row.id })
  ElMessage.success('修改成功')
}

function handleUpdate(row: FlashSession) {
  dialogVisible.value = true
  flashSession.value = { ...row }
  flashSession.value.startTime = row.startTime
  flashSession.value.endTime = row.endTime
}

async function handleDelete(row: FlashSession) {
  await ElMessageBox.confirm('是否要删除该时间段?', '提示', { type: 'warning' })
  await deleteSession(row.id)
  ElMessage.success('删除成功')
  getList()
}

async function handleDialogConfirm() {
  if (route.query.id) {
    await updateSession(flashSession.value)
    ElMessage.success('添加成功！')
    dialogVisible.value = false
    getList()
  } else {
    await createSession(flashSession.value)
    ElMessage.success('添加成功！')
    dialogVisible.value = false
    getList()
  }
}

async function getList() {
  listLoading.value = true
  try {
    const response = await fetchList()
    list.value = response.data.data
  } finally {
    listLoading.value = false
  }
}
</script>
<style scoped>
.operate-container {
  margin-top: 0;
}
</style>
